using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections;
using System.Configuration;

namespace SymAutomate
{
    /// <summary>
    /// Error Numbers 000, 001-019; Reference file ErrorNumbers.txt for more information.
    /// 000 Success
    /// 010 Error: Unhandled Exception %exception message%
    /// 011 Error: No Driver responds to Command '%command name%'.
    /// </summary>
    class Program
    {
        #region Class Variables
        private static ConsoleArguments _ConsoleArguments;
        private static HostTelnetConnection.Driver _HostTelnetConnectionDriver;
        #endregion

        static int Main(string[] args)
        {
            try
            {
                if (args.Length == 0 || args.Length == 1 && (args[0] == "/h" || args[0] == "/help" || args[0] == "help" || args[0] == "?"))
                {
                    Console.WriteLine(ReturnGlobalHelpText());
                    return 1;
                }

                _ConsoleArguments = new ConsoleArguments(args);
                _HostTelnetConnectionDriver = new SymAutomate.HostTelnetConnection.Driver(_ConsoleArguments);

                if (new SymAutomate.Util.Driver().RespondsToCommand(_HostTelnetConnectionDriver.Parameters.Command))
                {
                    SymAutomate.Util.Driver _UtilDriver = new SymAutomate.Util.Driver(_ConsoleArguments, _HostTelnetConnectionDriver);
                    _UtilDriver.ProcessCommand();
                }

                else if (new SymAutomate.JobFile.Driver().RespondsToCommand(_HostTelnetConnectionDriver.Parameters.Command))
                {
                    SymAutomate.JobFile.Driver _JobFileDriver = new SymAutomate.JobFile.Driver(_ConsoleArguments, _HostTelnetConnectionDriver);
                    _JobFileDriver.ProcessCommand();
                }

                else if (new SymAutomate.PcInterfaceControl.Driver().RespondsToCommand(_HostTelnetConnectionDriver.Parameters.Command))
                {
                    SymAutomate.PcInterfaceControl.Driver _PcInterfaceControlDriver = new SymAutomate.PcInterfaceControl.Driver(_ConsoleArguments, _HostTelnetConnectionDriver);
                    _PcInterfaceControlDriver.ProcessCommand();
                }

                else if (new SymAutomate.LetterFile.Driver().RespondsToCommand(_HostTelnetConnectionDriver.Parameters.Command))
                {
                    SymAutomate.LetterFile.Driver _LetterFileDriver = new SymAutomate.LetterFile.Driver(_ConsoleArguments, _HostTelnetConnectionDriver);
                    _LetterFileDriver.ProcessCommand();
                }

                else if (new SymAutomate.RawCommand.Driver().RespondsToCommand(_HostTelnetConnectionDriver.Parameters.Command))
                {
                    SymAutomate.RawCommand.Driver _RawCommandDriver = new SymAutomate.RawCommand.Driver(_ConsoleArguments, _HostTelnetConnectionDriver);
                    _RawCommandDriver.ProcessCommand();
                }

                else
                {
                    throw new ReturnException("011 Error: No Driver responds to Command '" + _HostTelnetConnectionDriver.Parameters.Command + "'.");
                }

                WriteMessage(0, "Success");
                return 0;
            }
            catch (ReturnException _ReturnException)
            {
                int _ErrorNumber = Convert.ToInt32(_ReturnException.Message.Substring(0, 3));
                string _ErrorDescription = _ReturnException.Message.Substring(4, _ReturnException.Message.Length - 4);
                WriteMessage(_ErrorNumber, _ErrorDescription);
                return _ErrorNumber;
            }
            catch (Exception _GlobalException)
            {
                WriteMessage(10, "Error - Unhandled Exception" + Environment.NewLine + _GlobalException.ToString().Replace(@"C:\MyFiles\SymAutomate\",""));
                return 10;
            }
        }

        private static void WriteMessage(int _ErrorNumber, string _ErrorDescription)
        {
            Console.WriteLine(_ErrorNumber.ToString().PadLeft(3, '0') + " - " + _ErrorDescription);
        }

        private static string ReturnGlobalHelpText()
        {
            string _ReturnValue = string.Empty;

            _ReturnValue += Environment.NewLine;
            _ReturnValue += "------------------------------------------------------------------------------" + Environment.NewLine;
            _ReturnValue += "SymAutomate.exe - Build Version " + SymAutomate.Util.Driver.GetVersion() + "; GNU General Public License" + Environment.NewLine;
            _ReturnValue += "------------------------------------------------------------------------------" + Environment.NewLine + Environment.NewLine;
            
            _ReturnValue += "Usage --  Example: SymAutomate.exe Command=RunJobFile JobFileName=MY.JOBFILE" + Environment.NewLine + Environment.NewLine;

            string _ResponseText = null;

            _ResponseText = ReturnItemHelpText(new SymAutomate.Util.Driver().ConsoleHelpList);
            if (_ResponseText != null) _ReturnValue += _ResponseText;

            _ResponseText = ReturnItemHelpText(new SymAutomate.JobFile.Driver().ConsoleHelpList);
            if (_ResponseText != null) _ReturnValue += _ResponseText;

            _ResponseText = ReturnItemHelpText(new SymAutomate.PcInterfaceControl.Driver().ConsoleHelpList);
            if (_ResponseText != null) _ReturnValue += _ResponseText;

            _ResponseText = ReturnItemHelpText(new SymAutomate.LetterFile.Driver().ConsoleHelpList);
            if (_ResponseText != null) _ReturnValue += _ResponseText;

            _ResponseText = ReturnItemHelpText(new SymAutomate.RawCommand.Driver().ConsoleHelpList);
            if (_ResponseText != null) _ReturnValue += _ResponseText;

            _ReturnValue += "------------------------------------------------------------------------------";

            return _ReturnValue;
            
        }

        private static string ReturnItemHelpText(List<ConsoleHelp> _ConsoleHelpList)
        {
            if (_ConsoleHelpList == null) return null;

            string _ResponseText = string.Empty;

            foreach (ConsoleHelp _ConsoleHelp in _ConsoleHelpList)
            {
                _ResponseText += "Command: " + _ConsoleHelp.CommandName + "; ";
                _ResponseText += "Parameters: ";
                for (int i = 0; i < _ConsoleHelp.Parameters.Count; i++)
                {
                    _ResponseText += _ConsoleHelp.Parameters[i];
                    if (i < _ConsoleHelp.Parameters.Count - 1)
                        _ResponseText += ", ";
                }
                if (_ConsoleHelp.Parameters.Count == 0)
                    _ResponseText += "None";
                _ResponseText += Environment.NewLine + Environment.NewLine;
            }

            return _ResponseText;
        }
    }
}
